//
//  HelpTipsVC.m
//  Yihubang
//
//  Created by hyu on 16/9/12.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "HelpTipsVC.h"

@interface HelpTipsVC (){
    NSTimer *timers;
    int sumTime;
    UIImageView *navBarHairlineImageView;
}

@end

@implementation HelpTipsVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    self.navigationController.navigationBar.backgroundImageColor = [UIColor colorWithHex:0x292C36];
    sumTime = 5;
    [self addTimeLabel];
    timers = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setTimeLabelText:) userInfo:nil repeats:YES];
    // Do any additional setup after loading the view.
    
}



-(void)viewWillAppear:(BOOL)animated{
    navBarHairlineImageView.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    navBarHairlineImageView.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    [self stopTimer];
}

- (void)setTimeLabelText:(NSTimer *)timer{
    sumTime -= 1;
    [self addTimeLabel];
    if (sumTime <= 0) {
        [self stopTimer];
        @weakify(self)
        [[[HttpManagerCenter sharedHttpManager] wantHelpWithresultClass:nil] subscribeNext:^(BaseModel *model){
            @strongify(self)
            if(model.code==200){
                [self.userClient setOpenTypeStatus:@[@"2",model.data[@"id"]]];
                UIViewController *vct = [UIViewController viewControllerWithStoryboard:@"GuardMe" identifier:@"StartUrgentVC" ];
                vct.params =@{@"come":@"2",@"helpId":model.data[@"id"],@"message":model.data[@"share_data"]};
                [self pushViewController:vct];
                
            }else if (model.code ==2046){
                [self hiddenHUDWithString:model.message error:NO];
                [self setMyContract];
            }else{
                [self hiddenHUDWithString:model.message error:NO];
            }
        }];
    }
}

- (void)addTimeLabel{
    UILabel* animationLable = [[UILabel alloc] initWithFrame:CGRectMake(0,100, 100, 100)];
    
    animationLable.centerX = self.view.centerX;
    animationLable.centerY = self.view.centerY-50;
    
    animationLable.backgroundColor = [UIColor colorWithHex:0x761064];
    
    animationLable.layer.cornerRadius = 50;
    
    animationLable.layer.masksToBounds = YES;
    
    UILabel* timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100, 100, 100)];
    
    timeLabel.centerX = self.view.centerX;
    timeLabel.centerY = self.view.centerY-50;
    
    timeLabel.textColor = [UIColor redColor];
    
    timeLabel.text = [NSString stringWithFormat:@"%d",sumTime];
    
    timeLabel.textAlignment = NSTextAlignmentCenter;
    
    timeLabel.font = [UIFont boldSystemFontOfSize:66.0];
    
    timeLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:animationLable];
    [self.view addSubview:timeLabel];
    [UIView animateWithDuration:3.0 animations:^{
        CGAffineTransform newTransform = CGAffineTransformMakeScale(3.0, 3.0);
        [animationLable setTransform:newTransform];
        [animationLable setAlpha:0];
    }
                     completion:^(BOOL finished){
                         [animationLable removeFromSuperview];
                     }];
    [UIView animateWithDuration:1.0 animations:^{
        CGAffineTransform newTransform = CGAffineTransformMakeScale(0.5, 0.5);
        [timeLabel setTransform:newTransform];
        [timeLabel setAlpha:0];
    }
                     completion:^(BOOL finished){
                         [timeLabel removeFromSuperview];
                     }];
    
}

-(void)setMyContract{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
    UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"MyContactVC"];
    [self pushViewController:myView animated:YES];
}
- (void)stopTimer{
    if (timers.valid) {
        [timers invalidate];
        timers = nil;
    }
}

- (IBAction)cancelHelp:(id)sender {
    [self gotoBack];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
